const util = require('../../utils/util.js');
const api = require('../../config/api.js');
//index.js
//获取应用实例
const app = getApp()
console.log(app)
Page({
  data: {
    scrollHeight: '',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    currentFilterId: 0, // 默认的筛选条件
    scrollTop: 0,
    tabItems: [
      { title: '区域', filterId: 1 },
      { title: '租金', filterId: 2 },
      { title: '排序', filterId: 3 }
      // { title: '房东', filterId: 4 }
    ],
    tabs: [true, true, true],
    pagination: {
      page: 1,
      size: 5,
      order_price: 0,
      update_time: 0,
      price: 0,
      province: '',
      city: '',
      country: ''
    },
    totalPage: 0,
    scrollTop: 0,
    hidden: false,
    pageTip: '加载中...',
    noData: false,
    items: [],
    currentChoose: 0,
    provinces: [],
    cities: [],
    country: []
  },
  /**
   * 切换筛选条件
  */
  chooseFilter: function (event) {
    if (+event.currentTarget.dataset.item.filterId === +this.data.currentFilterId) {
      return this.setData({
        currentFilterId: 0,
        scrollTop: 0
      });
    }
    this.setData({
      currentFilterId: event.currentTarget.dataset.item.filterId,
      scrollTop: 1
    });
  },
  //事件处理函数
  bindViewTap: function () {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function () {
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          scrollHeight: res.windowHeight
        });

      }
    });
    that.getProvinces();
    that.getList();

  },
  // bindscrolltolower: function () {
  //   var that = this;
  //   that.getList();
  // },
  getUserInfo: function (e) {
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  goPage: function () {
    if (app.globalData.userInfo.id) {
      wx.navigateTo({
        url: '/pages/public-item/public-item'
      })
    } else {
      wx.navigateTo({
        url: './../authorized/index'
      });
    }
  },
  /**
   * 绑定滚动事件
  */
  bindDownLoad: function (event) {
    let self = this;
    if (self.data.pagination.page == self.data.totalPage) {
      self.setData({
        noData: true
      });
      return false;
    }
    if (self.data.pagination.page != self.data.totalPage) {
      self.data.pagination.page += 1;
    }
    this.getList();
  },
  /**
   * 获取房间列表api
  */
  getList: function () {
    let self = this;
    self.setData({
      hidden: false
    });
    util.request(api.roomListUrl, self.data.pagination, 'post').then(function (res) {
      if (res.errno === 0) {
        self.data.items.push.apply(self.data.items, res.data.result.data);
        self.setData({
          items: self.data.items,
        });
        self.data.totalPage = res.data.result.totalPages
      }
      self.setData({
        hidden: true
      });
    });
  },
  /**
   * 租金排序筛选
   * @ param 回掉结果值
  */
  onGetOrder: function (e) {
    this.setData({
      currentFilterId: e.detail.currentFilterId,
      scrollTop: 0
    });
    this.data.pagination.order_price = e.detail.orderPrice;
    this.data.pagination.page = 1;
    var self = this;
    self.data.items = [];
    self.setData({
      items: [],
    });
    this.getList();
  },
  /**
   * 租金筛选
   * @ param 回掉结果值
  */
  onGetPrice: function (e) {
    this.data.items = [];
    this.setData({
      currentFilterId: e.detail.currentFilterId,
      scrollTop: 0,
      items: [],
      currentChoose: e.detail.currentChoose
    });
    this.data.pagination.page = 1;
    this.data.pagination.price = e.detail.price;
    this.getList();
  },

  /**
   * 获取所有省份
  */
  getProvinces: function () {
    var self = this;
    util.request(
      api.provinceListUrl,
      'post').then(function (res) {
        if (res.errno === 0) {
          self.setData({
            provinces: res.data.result
          })
        }
      });
  },

  /**
   * 获取城市
  */
  getCities: function (e) {
    var provinceId = e.currentTarget.dataset.id
    var self = this;
    util.request(
      api.cityeListUrl,
      { provinceId: provinceId },
      'post').then(function (res) {
        if (res.errno === 0) {
          self.setData({
            cities: res.data.result,
            country: []
          })
        }
      });
      
  },

  /**
   * 获取地图
  */
  getCountry: function (e) {
    var cityId = e.currentTarget.dataset.id
    var self = this;
    util.request(
      api.countryListUrl,
      { cityId: cityId },
      'post').then(function (res) {
        if (res.errno === 0) {
          self.setData({
            country: res.data.result
          })
        }
      });
  },
  searchRoom: function (e) {
    this.data.pagination.country = e.currentTarget.dataset.name;
    this.data.items = [];
    this.setData({
      scrollTop: 0,
      items: [],
      currentFilterId: 0 // 默认的筛选条件
    });
    this.data.pagination.page = 1;
    this.getList();
  }
})
